//
//  CustomerAndCar.m
//  ICrEAM
//
//  Created by Cty on 2016/11/06.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CustomerAndCar.h"
#import "CommonViewGenerator.h"
#import "ProvinceCityRespose.h"


#define Width ScreenWidth - 2*ActualWidth(78)
#define HeightLine1 ActualHeight(85)
#define HeightLine2 ActualHeight(140)
@interface CustomerAndCar()

@property (nonatomic,strong) ICMPersonerModel *personData;
@property (nonatomic,strong) ICMCarModel *carData;

@end

@implementation CustomerAndCar
+(instancetype)creatViewWithFrame:(CGRect)frame andPersonModel:(ICMPersonerModel *)model{
    return [[CustomerAndCar alloc]initWithFrame:frame andPersonModel:model];
}
+(instancetype)creatViewWithFrame:(CGRect)frame andCarModel:(ICMCarModel *)model{
    return [[CustomerAndCar alloc]initWithFrame:frame andCarModel:model];
}
-(instancetype)initWithFrame:(CGRect)frame andCarModel:(ICMCarModel *)model{
    self  = [super initWithFrame:frame];
    
    if(self){
        
        self.carData = model;
        
        [self setUpCarViews];
        
    }
    return self;
    
    
}
-(instancetype)initWithFrame:(CGRect)frame andPersonModel:(ICMPersonerModel *)model{
    self  = [super initWithFrame:frame];
    
    if(self){
        
        self.personData = model;
        
        [self setUpPersonViews];
    
    }
    return self;
    

}
- (void)setUpCarViews{
    //Block1
    NSArray *block1 = [[NSArray alloc]initWithObjects:
                       [@"VIN*ICM*" stringByAppendingString:self.carData.vinNum?:@""],
                       [@"品牌*ICM*" stringByAppendingString:self.carData.brandName?:@""],
                       [@"车系*ICM*" stringByAppendingString:self.carData.carKeiName?:@""],
                       [@"车型*ICM*" stringByAppendingString:self.carData.carTypeName?:@""],
                       [@"本公司购车*ICM*" stringByAppendingString:[self.carData.isOurBranch isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)]?@"是":@"否"],
                       nil];
    
    CommonViewGenerator *generator1 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, 0, Width, HeightLine2) andDataSource:block1 andIsLast:NO];
    
    [self addSubview:generator1];
    


    //Block2
    NSArray *block2 = [[NSArray alloc]initWithObjects:
                       [@"车牌号*ICM*" stringByAppendingString:self.carData.carNum?:@""],
                       [@"使用性质*ICM*" stringByAppendingString:!StrIsZero_gdm(self.carData.useNature) ?([ICrEAMTool getCodeModelForCode:self.carData.useNature].desc?:@""):@""],
                       [@"车辆性质*ICM*" stringByAppendingString:!StrIsZero_gdm(self.carData.carNature)?[ICrEAMTool getCodeModelForCode:self.carData.carNature].desc:@""],
                       [@"车辆用途*ICM*" stringByAppendingString:!StrIsZero_gdm(self.carData.carUses)?[ICrEAMTool getCodeModelForCode:self.carData.carUses].desc:@""],
                       [@"购车发票日期*ICM*" stringByAppendingString:!StrIsZero_gdm(self.carData.parchaseDate)?[ICrEAMTool getDateWithTimeInterval:self.carData.parchaseDate]:@""],
                       [@"当前行驶里程*ICM*" stringByAppendingString:self.carData.lastMillions?:@""],
                       [@"日均行驶里程*ICM*" stringByAppendingString:self.carData.perMillions?:@""],
                       nil];
    
    CommonViewGenerator *generator2 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, generator1.bottomEdge, Width, HeightLine2) andDataSource:block2 andIsLast:NO];
    
    [self addSubview:generator2];

    
    //Block3
    NSArray *block3 = [[NSArray alloc]initWithObjects:
                       [@"保修结束日期*ICM*" stringByAppendingString:!StrIsZero_gdm(self.carData.guaranteeEndDate)?[ICrEAMTool getDateWithTimeInterval:self.carData.guaranteeEndDate]:@""],
                       [@"保修结束里程*ICM*" stringByAppendingString:self.carData.guaranteeEndMillions?:@""],
                       [@"下次验车日期*ICM*" stringByAppendingString:!StrIsZero_gdm(self.carData.nextCheckDate)?[ICrEAMTool getDateWithTimeInterval:self.carData.nextCheckDate]:@""],
                       nil];
    
    CommonViewGenerator *generator3 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, generator2.bottomEdge, Width, HeightLine1) andDataSource:block3 andIsLast:NO];
    
    [self addSubview:generator3];

    
    //Block4
    NSArray *block4 = [[NSArray alloc]initWithObjects:
                       [@"保险公司*ICM*" stringByAppendingString:self.carData.insuranceCompany?:@""],
                       [@"保险结束日期*ICM*" stringByAppendingString:!StrIsZero_gdm(self.carData.insuranceEndDate)?[ICrEAMTool getDateWithTimeInterval:self.carData.insuranceEndDate]:@""],

                       nil];
    
    CommonViewGenerator *generator4 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, generator3.bottomEdge, Width, HeightLine1) andDataSource:block4 andIsLast:YES];
    
    [self addSubview:generator4];

    
}

- (void)setUpPersonViews{
    //Block1
    

    NSArray *block1 = [[NSArray alloc]initWithObjects:
                       [@"车主姓名*ICM*" stringByAppendingString:self.personData.ownerName?:@""],
                       [@"性别*ICM*" stringByAppendingString:!StrIsZero_gdm(self.personData.gender)?([ICrEAMTool getCodeModelForCode:self.personData.gender].desc?:@""):@""],
                       [@"出生日期*ICM*" stringByAppendingString:self.personData.birthday?[ICrEAMTool getDateWithTimeInterval:self.personData.birthday]:@""],
                       [@"车主性质*ICM*" stringByAppendingString:!StrIsZero_gdm(self.personData.ownerType)?([ICrEAMTool getCodeModelForCode:self.personData.ownerType].desc?:@""):@""],
                       [@"证件类型*ICM*" stringByAppendingString:!StrIsZero_gdm(self.personData.cerType)?([ICrEAMTool getCodeModelForCode:self.personData.cerType].desc?:@""):@""],
                       [@"证件号码*ICM*" stringByAppendingString:self.personData.cerNum?:@""],
     nil];
    
    CommonViewGenerator *generator1 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, 0, Width, HeightLine2) andDataSource:block1 andIsLast:NO];
    
    [self addSubview:generator1];
    
    //Block2
    NSArray *block2 = [[NSArray alloc]initWithObjects:
                       [@"所在省份*ICM*" stringByAppendingString:[self getCityDataWithCode:self.personData.province Type:0]?:@""],
                       [@"城市*ICM*" stringByAppendingString:[self getCityDataWithCode:self.personData.city Type:1]?:@""],
                       [@"区县*ICM*" stringByAppendingString:[self getCityDataWithCode:self.personData.district Type:2]?:@""],
                       [@"手机*ICM*" stringByAppendingString:self.personData.phoneNum?:@""],
                       [@"地址*ICM*" stringByAppendingString:self.personData.address?:@""],
                       nil];
    
    CommonViewGenerator *generator2 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, generator1.bottomEdge, Width, HeightLine2) andDataSource:block2 andIsLast:NO];
    
    [self addSubview:generator2];
    
    //Block3
    NSArray *block3 = nil;
    if([ICrEAMTool isVW])
    {
        block3 = [[NSArray alloc]initWithObjects:
                  [@"专属服务顾问*ICM*" stringByAppendingString:self.personData.adviserName?:@""],
                  [@"大众一家会员*ICM*" stringByAppendingString:self.personData.vwOneHome?:@""],
                  [@"会员编号*ICM*" stringByAppendingString:self.personData.oneHomeNum?:@""],
                  nil];
    }
    else
    {
        block3 = [[NSArray alloc]initWithObjects:
                  [@"专属服务顾问*ICM*" stringByAppendingString:self.personData.adviserName?:@""],
                  [@"会员编号*ICM*" stringByAppendingString:self.personData.oneHomeNum?:@""],
                  nil];
    }
    
    CommonViewGenerator *generator3 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, generator2.bottomEdge, Width, HeightLine1) andDataSource:block3 andIsLast:NO];
    
    [self addSubview:generator3];
    
    //爱好
    __block NSMutableString *hobby = [[NSMutableString alloc] init];
    NSArray *hobbysArray = [self.personData.hobby componentsSeparatedByString:@";"];
    if(self.personData.hobby && ![self.personData.hobby isEqualToString:@""] && hobbysArray && hobbysArray.count > 0)
    {
        for(NSString *str in hobbysArray)
        {
            NSString *temp = [ICrEAMTool getCodeModelForCode:str].desc;
            if(temp && ![temp isEqualToString:@""])
            {
                [hobby appendString:temp];
                [hobby appendString:@";"];
            }
        }
    }
    
    //Block4
    NSArray *block4 = [[NSArray alloc]initWithObjects:
                       [@"行业小类*ICM*" stringByAppendingString:!StrIsZero_gdm(self.personData.district)?([ICrEAMTool getCodeModelForCode:self.personData.profType].desc?:@""):@""],
                       [@"家庭月收入*ICM*" stringByAppendingString:[ICrEAMTool getCodeModelForCode:self.personData.familyIncome].desc?:@""],
                       [@"爱好*ICM*" stringByAppendingString:self.personData.hobby?:@""],
                       [@"消费特征*ICM*" stringByAppendingString:!StrIsZero_gdm(self.personData.district)?([ICrEAMTool getCodeModelForCode:self.personData.consumeProperty].desc?:@""):@""],
                       [@"行为特征*ICM*" stringByAppendingString:!StrIsZero_gdm(self.personData.district)?([ICrEAMTool getCodeModelForCode:self.personData.mannerProperty].desc?:@""):@""],
                       [@"喜欢的礼品*ICM*" stringByAppendingString:self.personData.interestedPresent?:@""],
                       [@"感兴趣的活动*ICM*" stringByAppendingString:self.personData.interestedActivity?:@""],
                       nil];
    
    CommonViewGenerator *generator4 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, generator3.bottomEdge, Width, HeightLine2) andDataSource:block4 andIsLast:NO];
    
    [self addSubview:generator4];
    

    //Block5
    NSArray *block5 = [[NSArray alloc]initWithObjects:
                       [@"预收款*ICM*" stringByAppendingString:self.personData.advancedReceived?:@""],
                       [@"欠款金额*ICM*" stringByAppendingString:self.personData.amount?:@""],
                       nil];
    
    CommonViewGenerator *generator5 = [CommonViewGenerator creatGeneratoredViewWithFrame:CGRectMake(0, generator4.bottomEdge, Width, HeightLine2) andDataSource:block5 andIsLast:YES];
    
    [self addSubview:generator5];

}


//根据code来查找省市县区
- (NSString *)getCityDataWithCode:(NSString *)code Type:(int)type
{
    NSArray *cityDataArray = [ICrEAMTool getProvinceCityArray];
    if(type == 0)
    {
        //查询省
        for(int i = 0;i<cityDataArray.count;i++)
        {
            NSArray *tempProvinceArray = cityDataArray[i];
            ProvinceModel *provinceData = tempProvinceArray[0];
            if([provinceData.provinceCode isEqualToString:code])
            {
                return provinceData.provinceName;
            }
            
        }
    }
    if(type == 1)
    {
        //查询市
        for(int i = 0;i<cityDataArray.count;i++)
        {
            NSArray *tempProvinceArray = cityDataArray[i];
            ProvinceModel *provinceData = tempProvinceArray[0];
            NSArray *tempCityArray = provinceData.cityList;
            for(int j = 0;j<tempCityArray.count;j++)
            {
                CityModel *citydata = tempCityArray[j];
                if([citydata.cityCode isEqualToString:code])
                {
                    return citydata.cityName;
                }
            }
            
        }
    }
    if(type == 2)
    {
        //查询县区
        for(int i = 0;i<cityDataArray.count;i++)
        {
            NSArray *tempProvinceArray = cityDataArray[i];
            ProvinceModel *provinceData = tempProvinceArray[0];
            NSArray *tempCityArray = provinceData.cityList;
            for(int j = 0;j<tempCityArray.count;j++)
            {
                CityModel *citydata = tempCityArray[j];
                NSArray *tempDisArray = citydata.districtyList;
                for(int z = 0;z<tempDisArray.count;z++)
                {
                    DistrictModel *disData = tempDisArray[z];
                    if([disData.distCode isEqualToString:code])
                    {
                        return disData.distName;
                    }
                }
            }
            
        }
    }
    return @"";
}

@end
